"""add_certificates_table

Revision ID: 63766cfa6a42
Revises: 024a0fe4cc9e
Create Date: 2025-06-18 22:05:36.719278

"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
import sqlmodel


# revision identifiers, used by Alembic.
revision: str = "63766cfa6a42"
down_revision: Union[str, None] = "024a0fe4cc9e"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "certificates",
        sa.Column("id", sa.Uuid(), nullable=False),
        sa.Column(
            "certificate_type",
            sa.Enum("ROOT", "INTERMEDIATE", "USER", "AGENT", name="certificatetype"),
            nullable=False,
        ),
        sa.Column("serial_number", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("subject", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("issuer", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column(
            "status",
            sa.Enum("PENDING", "VALID", "REVOKED", "EXPIRED", name="certificatestatus"),
            nullable=False,
        ),
        sa.Column("issued_at", sa.DateTime(), nullable=False),
        sa.Column("expires_at", sa.DateTime(), nullable=False),
        sa.Column("revoked_at", sa.DateTime(), nullable=True),
        sa.Column(
            "revocation_reason", sqlmodel.sql.sqltypes.AutoString(), nullable=True
        ),
        sa.Column(
            "certificate_pem", sqlmodel.sql.sqltypes.AutoString(), nullable=False
        ),
        sa.Column("public_key", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("parent_certificate_id", sa.Uuid(), nullable=True),
        sa.Column("aic", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(
            ["parent_certificate_id"],
            ["certificates.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_certificates_aic"), "certificates", ["aic"], unique=False)
    op.create_index(
        op.f("ix_certificates_serial_number"),
        "certificates",
        ["serial_number"],
        unique=True,
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f("ix_certificates_serial_number"), table_name="certificates")
    op.drop_index(op.f("ix_certificates_aic"), table_name="certificates")
    op.drop_table("certificates")
    # ### end Alembic commands ###
